package com.example.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests(authz -> authz
                        // 允许上传接口匿名访问
                        .requestMatchers("/files/**", "/register", "/login").permitAll()
                        .anyRequest().authenticated()
                )
                .csrf(csrf -> csrf
                        // 如果不需要CSRF保护，可以禁用或配置排除上传接口
                        .ignoringRequestMatchers("/files/upload", "/register", "/login")
                );
        return http.build();
    }
}
